package com.aoxon.cargo.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.cache.SupCache;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.service.GetClothNumBerforSupplierService;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.state.DataState;
import com.aoxon.cargo.state.MyStateUtil;

public class SupIndexActivity2 extends NewBaseActivity {

	private ImageView ivReturnButton, ivLogo, ivCertification;
	private TextView tvTitle, tvStoreName, tvAddress, tvFastAddGood,
			tvShopSetting, tvCargoShop;
	private ProgressBar pbLogo;
	private View vShopManage, vBannerManage, vSureShop;

	private FileLoader imageLoader = new FileLoader(200, 200, false);
	private Supplier supplier;
	private GetClothNumBerforSupplierService mGetClothNumBerforSupplierService = 
			(GetClothNumBerforSupplierService)MyServiceFactory
			.getInstance(MyServiceFactory.GETCLOTHNUMBERFORSUPPLIERSERVICE);

	private static List<View> manageViews = new ArrayList<View>();

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.sup_index_2;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		ivReturnButton = (ImageView) findViewAndSetOnClick(R.id.ivPublicTitleReturn);
		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		ivLogo = (ImageView) findViewById(R.id.ivSupplierLogo);
		tvStoreName = (TextView) findViewById(R.id.tvSupplierStoreName);
		tvAddress = (TextView) findViewById(R.id.tvSupplierStoreAddress);
		pbLogo = (ProgressBar) findViewById(R.id.pbSupplierLogoLoading);
		ivCertification = (ImageView) findViewById(R.id.ivSupplierCertification);
		tvFastAddGood = (TextView) findViewAndSetOnClick(R.id.tvSupIndexQuickAddButton);
		vShopManage = findViewAndSetOnClick(R.id.ly_shop_manage);
		vBannerManage = findViewAndSetOnClick(R.id.ly_banner_manage);
		vSureShop = findViewAndSetOnClick(R.id.ly_sure_shop);
		tvCargoShop = (TextView) findViewAndSetOnClick(R.id.tv_cargo_shop);
		tvShopSetting = (TextView) findViewAndSetOnClick(R.id.tv_shop_setting);

		supplier = SupIndexShared.getSupplier();
		tvTitle.setText("供应商");
		showSupplier(supplier);
		initManageViews();
		load();
	}

	public void showSupplier(Supplier supplier) {
		tvStoreName.setText(supplier.getStoreName().toString());
		tvAddress.setText(supplier.getSupCity() + supplier.getsupMarket()
				+ supplier.getsupStall());
		if (DataState.SUCCESS == supplier.getCerState()) {
			ivCertification.setVisibility(View.VISIBLE);
		}
		try {

			if (supplier.getSupLogo() != null
					&& supplier.getSupLogo().get(0).getPictureState() == DataState.SUCCESS) {
				String path = supplier.getSupLogo().get(0).getPictureUrl()
						+ "s.png";
				imageLoader.displayImage(path, ivLogo, pbLogo, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pbLogo.setVisibility(View.GONE);
		}
	}

	public void initManageViews() {
		manageViews.clear();
		manageViews.add(vShopManage);
		manageViews.add(vBannerManage);
		manageViews.add(tvShopSetting);
		manageViews.add(vSureShop);
		manageViews.add(tvCargoShop);

		String[] text = { "商品管理", "Banner管理", "店铺设置", "申请认证", "拿货微店" };
		int[] img = { R.drawable.shop_car, R.drawable.pic, 0,
				R.drawable.sure_sup, 0 };

		int manageViewLength = manageViews.size();
		for (int i = 0; i < manageViewLength; i++) {
			if (img[i] != 0) {
				View child = manageViews.get(i);
				TextView tv = (TextView) child.findViewById(R.id.tv_center);
				ImageView iv = (ImageView) child.findViewById(R.id.iv_center);
				tv.setText(text[i]);
				iv.setImageDrawable(this.getResources().getDrawable(img[i]));
			} else {
				TextView tv = (TextView) manageViews.get(i);
				tv.setText(text[i]);
			}
		}
		TextView tv = (TextView) vShopManage.findViewById(R.id.tv_count);
		tv.setVisibility(View.VISIBLE);
		tv.setText("" + supplier.getClothNum());
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		Intent intent = null;

		switch (view.getId()) {

		case R.id.ivPublicTitleReturn:
			CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
			intent = new Intent(getBaseContext(), CargoActivity.class);
			startActivity(intent);
			break;

		case R.id.tvSupIndexQuickAddButton:
			intent = new Intent(getBaseContext(), SupCameraActivity.class);
			SupCache.pictureList.clear();
			SupInputGoodsInfoActivity.TO_ADD_GOODS = true;
			SupCache.from = 0;
			startActivity(intent);
			break;
		case R.id.ly_shop_manage:
			
			intent = new Intent(SupIndexActivity2.this,SupGoodsManagerActivity.class);
			startActivity(intent);
			break;
		case R.id.ly_banner_manage:
			intent = new Intent(SupIndexActivity2.this,BannerManageActivity.class);
			startActivity(intent);
			break;
		case R.id.ly_sure_shop:
			intent = new Intent(SupIndexActivity2.this,SupCertificationActivity.class);
			startActivity(intent);
			break;
		case R.id.tv_cargo_shop:
			break;
		case R.id.tv_shop_setting:
			intent = new Intent(SupIndexActivity2.this,SupSettingActivity.class);
			startActivity(intent);
			break;
		/*
		 * case R.id.rlSupIndexGoodsManagement: intent = new
		 * Intent(getBaseContext(), SupGoodsManagerActivity.class);
		 * startActivity(intent); break;
		 * 
		 * case R.id.rlSupIndexSystemMessages: intent = new
		 * Intent(getBaseContext(), SupSystemMessageListActivity.class);
		 * startActivity(intent); break;
		 */

		default:
			break;
		}
		SupIndexActivity2.this.finish();
	}

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub

	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		try {
			gsonBean = mGetClothNumBerforSupplierService.execute(supplier.getSupId());
			Log.v(" SupIndexActivity 22222 ", gson.toJson(gsonBean));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub

	}

}
